package com.example.pei.ithinklearnt.view.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.mViewPageAdapter;
import com.example.pei.ithinklearnt.view.activity.SearchUserActivity;
import com.example.pei.ithinklearnt.view.fragments.innerfragments.FriendsFragmet;
import com.example.pei.ithinklearnt.view.fragments.innerfragments.MessageListsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Pei on 2017/12/6.
 */

public class MessageFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private View view;

    private List<Fragment> fragments=new ArrayList<Fragment>();
    private mViewPageAdapter messageViewPageAdapter;;


    @BindView(R.id.rg_choice)
    RadioGroup radioGroupChoice;

    @BindView(R.id.rb_message)
    RadioButton radioButton_message;
    @BindView(R.id.rb_friend)
    RadioButton radioButton_friend;
    @BindView(R.id.fragment_container)
    ViewPager viewPager;




    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        if (checkedId==radioButton_friend.getId()){      //好友界面
            if (null == fragments.get(1)) {
                fragments.set(1,new FriendsFragmet());
            }
            viewPager.setCurrentItem(1);
        }else if (checkedId==radioButton_message.getId()){  //聊天界面
            if (null == fragments.get(0)) {
                fragments.set(0,new MessageListsFragment());
            }
            viewPager.setCurrentItem(0);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.messagemenu_fragment, container, false);
        ButterKnife.bind(this, view);
        initFragments();



        return view;
    }
//剑网三 295JX3BA5RTUWG6612M
    private void initFragments(){
        radioGroupChoice.setOnCheckedChangeListener(this);  //设置监听

        fragments.add(new MessageListsFragment());
        fragments.add(new FriendsFragmet());
        String[] titles=new String[]{"111","222"};
        messageViewPageAdapter=new mViewPageAdapter(getChildFragmentManager(),fragments,titles);
        viewPager.setAdapter(messageViewPageAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override public void onPageScrollStateChanged(int state) {}
            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    radioButton_message.setChecked(true);
                }else{
                    radioButton_friend.setChecked(true);
                }
            }
        });
    }

//    @OnClick(R.id.btn_addfriend)
//    void addFriend(){
//
//
//        Intent intent = new Intent();
//        intent.setClass(getActivity(), SearchUserActivity.class);
//        startActivity(intent);
//    }


}
